package com.pactera.asmp.server.controller.user;

import com.alibaba.fastjson.JSONObject;
import com.pactera.asmp.server.common.constants.Constants;
import com.pactera.asmp.server.common.constants.PermissionCode;
import com.pactera.asmp.server.common.constants.ResultCode;
import com.pactera.asmp.server.common.exception.CustomException;
import com.pactera.asmp.server.entity.PermissionVO;
import com.pactera.asmp.server.entity.ResponseResult;
import com.pactera.asmp.server.entity.RoleDTO;
import com.pactera.asmp.server.entity.RoleVO;
import com.pactera.asmp.server.pojo.Permission;
import com.pactera.asmp.server.pojo.Role;
import com.pactera.asmp.server.pojo.RolePermissionKey;
import com.pactera.asmp.server.pojo.User;
import com.pactera.asmp.server.service.AuthService;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.List;

/**
 * @Description 普通请求统一使用error页面处理，异步请求，返回统一的Result(status,message,data)对象
 * @auther: lzq
 * @date: 2019/7/29 11:09
 */
@RestController
@RequestMapping("/auth")
public class AuthController {

	private static final Logger logger = LoggerFactory
			.getLogger(AuthController.class);
	@Autowired private AuthService authService;

	/**
	 * 添加权限【test】
	 * @param permission
	 * @return ok/fail
	 */

	@PostMapping(value = "/addPermission")
	@ResponseBody
	public ModelAndView toPage(@RequestBody Permission permission) {
		logger.debug("新增权限--permission-{}" , permission);
		ModelAndView mav = new ModelAndView("/home");
		try {
			if (null != permission) {
				permission.setInsertTime(new Date());

				authService.addPermission(permission);
				logger.debug("新增权限成功！-permission-{}", permission);
				mav.addObject("msg", "ok");
			}
		} catch (Exception e) {
			mav.addObject("msg", "fail");
			logger.error("新增权限异常！", e);
		}
		return mav;
	}

	/**
	 * 权限列表
	 * @return ok/fail
	 */
	@GetMapping(value = "/permList")
	@ResponseBody
	public ModelAndView permList() {
		logger.debug("权限列表！");
		ModelAndView mav = new ModelAndView("/auth/permList");
		try {
			List<Permission> permList = authService.permList();
			logger.debug("权限列表查询=permList:{}" , permList);
			mav.addObject("permList", permList);
			mav.addObject("msg", "ok");
		} catch (Exception e) {
			logger.error("权限查询异常！", e);
		}
		return mav;
	}

	/**
	 * 添加权限
	 * @param type [0：编辑；1：新增子节点权限]
	 * @param permission
	 * @return ModelAndView ok/fail
	 */
	/**
	 * @api {post} /auth/setPerm setPerm
	 * @apiDescription 添加/编辑权限
	 * @apiName add/edit Permission
	 * @apiGroup Permission manage
	 * @apiParam {int} type 区分添加/编辑权限
	 * @apiParam {json} data 权限数据
	 * @apiParamExample {json} Request-Example:
	 * {
	 *  type=1,
	 * 	Permission{
	 * 	id=null,
	 * 	name='1',
	 * 	pid=0,
	 * 	zindex=1,
	 * 	istype=0,
	 * 	descpt='1',
	 * 	code='1',
	 * 	icon='null',
	 * 	page='1',
	 * 	insertTime=null,
	 * 	updateTime=null
	 * 	}
	 * }
	 * @apiSuccessExample Success-Response:
	 * {
	 *     "ok"
	 * }
	 **/
	@RequiresPermissions({PermissionCode.PERMISSION_MANAGEMENT})
	@PostMapping(value = "/setPerm")
	@ResponseBody
    public ResponseResult setPerm(@RequestBody JSONObject jsonParam) {
	    ResponseResult responseResult;
	    int type = Integer.parseInt(jsonParam.get("type").toString());
        Permission permission = jsonParam.getObject("permission", Permission.class);
		try {
			if (null != permission && StringUtils.isNotEmpty(permission.getName())) {
			    if(!permission.getName().matches(Constants.REGEXP_FIRST_LAST_NOT_SPACE)) {
                    return new ResponseResult(ResultCode.PARAM_TYPE_BIND_ERROR);
			    }
				Date date = new Date();
				if (0 == type) {
					permission.setUpdateTime(date);
					//编辑权限
					this.authService.updatePerm(permission);
				} else if (1 == type) {
					permission.setInsertTime(date);
					//增加子节点权限
					this.authService.addPermission(permission);
				}
				logger.debug("设置权限成功！-permission-{}" , permission);
                responseResult = new ResponseResult(ResultCode.SUCCESS);
				return responseResult;
			} else {
                responseResult = new ResponseResult(ResultCode.PARAM_IS_BLANK);
                return responseResult;
            }
		} catch (CustomException e) {
		    return new ResponseResult(e.getResultCode());
		}catch (Exception e) {
			logger.error("设置权限异常！", e);
            responseResult = new ResponseResult(ResultCode.SYSTEM_INNER_ERROR);
            return responseResult;
		}
	}

	/**
	 * 获取权限
	 * @param id
	 * @return
	 */
	/**
	 * @api {get} /auth/getPerm getPerm
	 * @apiDescription 根据用户id获取权限
	 * @apiName getPerm
	 * @apiGroup Permission manage
	 * @apiParam {int} id 用户id
	 * @apiParamExample {json} Request-Example:
	 * {
	 * 	id=1
	 * }
	 * @apiSuccessExample Success-Response:
	 * {
	 * 	Permission{
	 * 	id=1,
	 * 	name='系统管理',
	 * 	pid=0,
	 * 	zindex=100,
	 * 	istype=0,
	 * 	descpt='ASMP系统管理',
	 * 	code='system',
	 * 	icon='systemManage.png',
	 * 	page='/',
	 * 	insertTime=Sun Jul 28 11:28:53 CST 2019,
	 * 	updateTime=Wed Jul 31 13:57:21 CST 2019
	 * 	}
	 * }
	 **/
	@GetMapping(value = "/getPerm")
	@ResponseBody
	public ResponseResult getPerm(@RequestBody JSONObject jsonParam) {
        Integer id = Integer.valueOf(jsonParam.get("id").toString());
	    ResponseResult responseResult = new ResponseResult(ResultCode.SUCCESS);
		logger.debug("获取权限--id-{}" , id);
		try {
			if (id > 0) {
				Permission perm = this.authService.getPermission(id);
				logger.debug("获取权限成功！-permission-{}" , perm);
				JSONObject jsonObject = new JSONObject();
				jsonObject.put("list", perm);
                responseResult.setData(jsonObject);
				return responseResult;
			}
		} catch (Exception e) {
            responseResult = new ResponseResult(ResultCode.SYSTEM_INNER_ERROR);
            responseResult.setMessage("获取权限异常！");
			logger.error("获取权限异常！", e);
		}
		return responseResult;
	}

	@RequiresPermissions(value = {PermissionCode.ROLE_MANAGEMENT, PermissionCode.PERMISSION_MANAGEMENT}, logical = Logical.OR)
    @GetMapping(value = "/getPerms")
    @ResponseBody
    public ResponseResult getPerms() {
        ResponseResult responseResult;
        try {
            List<Permission> permList = authService.permList();
            logger.debug("权限列表查询=permList:{}", permList);
            responseResult = new ResponseResult(ResultCode.SUCCESS);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("list", permList);
            responseResult.setData(jsonObject);
        } catch (Exception e) {
            responseResult = new ResponseResult(ResultCode.SYSTEM_INNER_ERROR);
            logger.error("权限查询异常！", e);
        }
        return responseResult;
    }



	/**
	 * 删除权限
	 * @param id
	 * @return
	 */
	/**
	 * @api {post} /auth/del del
	 * @apiDescription 根据用户id删除权限
	 * @apiName del
	 * @apiGroup Permission manage
	 * @apiParam {int} id 用户id
	 * @apiParamExample {json} Request-Example:
	 * {
	 * 	id=35
	 * }
	 * @apiSuccessExample Success-Response:
	 * {
	 * }
	 **/
	@RequiresPermissions({PermissionCode.PERMISSION_MANAGEMENT})
	@PostMapping(value = "/delPermission")
	@ResponseBody
	public ResponseResult delPermission(@RequestBody JSONObject jsonParam) {
        Integer id = Integer.valueOf(jsonParam.get("id").toString());
        ResponseResult responseResult;
		logger.debug("删除权限--id-{}" , id);
		try {
			if (id > 0) {
			    String msg = authService.delPermission(id);
			    if("ok".equals(msg)){
                    responseResult = new ResponseResult(ResultCode.SUCCESS);
                }else {
                    responseResult = new ResponseResult(ResultCode.FAIL);
                }
				return responseResult;
			}else{
                responseResult = new ResponseResult(ResultCode.PARAM_IS_INVALID);
            }
		} catch (Exception e) {
            responseResult = new ResponseResult(ResultCode.SYSTEM_INNER_ERROR);
            responseResult.setMessage("删除权限异常！");
			logger.error("删除权限异常！", e);
		}
		return responseResult;
	}

	/**
	 * 跳转到角色列表
	 * @return
	 */
	@GetMapping(path = "/roleManage")
	public ModelAndView toPage() {
		return new ModelAndView("/auth/roleManage");
	}

	/**
	 * 角色列表
	 * @return ok/fail
	 */
	/**
	 * @api {get} /auth/getRoleList getRoleList
	 * @apiDescription 获取角色列表
	 * @apiName getRoleList
	 * @apiGroup Role manage
	 * @apiSuccessExample Success-Response:
	 * {
	 * [
	 *     {
	 *         "id":1,
	 *         "roleName":"超级管理员",
	 *         "descpt":"超级管理员",
	 *         "code":"superman",
	 *         "insertUid":null,
	 *         "insertTime":1564284533000,
	 *         "updateTime":1565836798000
	 *     },
	 *     {
	 *         "id":2,
	 *         "roleName":"高级管理员",
	 *         "descpt":"高级管理员",
	 *         "code":"highmanage",
	 *         "insertUid":null,
	 *         "insertTime":1564284533000,
	 *         "updateTime":1565405507000
	 *     },
	 *     {
	 *         "id":3,
	 *         "roleName":"项目经理",
	 *         "descpt":"项目经理",
	 *         "code":"pm",
	 *         "insertUid":null,
	 *         "insertTime":1564284533000,
	 *         "updateTime":1565408595000
	 *     },
	 *     {
	 *         "id":4,
	 *         "roleName":"开发人员",
	 *         "descpt":"开发人员",
	 *         "code":"developers",
	 *         "insertUid":null,
	 *         "insertTime":1564284533000,
	 *         "updateTime":1565167369000
	 *     },
	 *     {
	 *         "id":5,
	 *         "roleName":"测试人员",
	 *         "descpt":"测试人员",
	 *         "code":"tester",
	 *         "insertUid":null,
	 *         "insertTime":1564284533000,
	 *         "updateTime":1565167311000
	 *     },
	 *     {
	 *         "id":6,
	 *         "roleName":"客户",
	 *         "descpt":"客户",
	 *         "code":"customer",
	 *         "insertUid":null,
	 *         "insertTime":1564284533000,
	 *         "updateTime":1565166472000
	 *     },
	 *     {
	 *         "id":7,
	 *         "roleName":"供应商",
	 *         "descpt":"供应商",
	 *         "code":"supplier",
	 *         "insertUid":null,
	 *         "insertTime":1564284533000,
	 *         "updateTime":1565167361000
	 *     },
	 *     {
	 *         "id":8,
	 *         "roleName":"产品经理",
	 *         "descpt":"项目PO，负责项目需求整理和ID发掘",
	 *         "code":"po",
	 *         "insertUid":null,
	 *         "insertTime":1564716518000,
	 *         "updateTime":1565167349000
	 *     },
	 *     {
	 *         "id":9,
	 *         "roleName":"测试经理",
	 *         "descpt":"测试部负责人",
	 *         "code":"TestManager",
	 *         "insertUid":null,
	 *         "insertTime":1564716685000,
	 *         "updateTime":1565167354000
	 *     }
	 * ]
	 * }
	 **/
	@RequiresPermissions(value = {PermissionCode.USER_MANAGEMENT, PermissionCode.ROLE_MANAGEMENT}, logical = Logical.OR)
	@GetMapping(value = "/getRoleList")
	@ResponseBody
	public ResponseResult getRoleList() {
		logger.debug("角色列表！");
        ResponseResult responseResult = new ResponseResult(ResultCode.SUCCESS);
		List<Role> roleList=null;
		try {
			roleList = authService.roleList();
			logger.debug("角色列表查询=roleList:{}", roleList);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("list",roleList);
            responseResult.setData(jsonObject);
		} catch (Exception e) {
			logger.error("角色查询异常！", e);
            responseResult = new ResponseResult(ResultCode.SYSTEM_INNER_ERROR);
		}
		return responseResult;
	}

	/**
	 * 查询权限树数据
	 * @return PermTreeDTO
	 */
	/**
	 * @api {get} /auth/findPerms findPerms
	 * @apiDescription 获得权限树列表
	 * @apiName findPerms
	 * @apiGroup Permission manage
	 * @apiSuccessExample Success-Response:
	 * {
	 * [
	 *     {
	 *         "id":"1",
	 *         "name":"系统管理",
	 *         "pId":"0",
	 *         "istype":"0",
	 *         "code":"system",
	 *         "page":"/",
	 *         "icon":"systemManage.png",
	 *         "zindex":"100",
	 *         "checked":false,
	 *         "open":true
	 *     },
	 *     {
	 *         "id":"2",
	 *         "name":"用户管理",
	 *         "pId":"1",
	 *         "istype":"0",
	 *         "code":"usermanage",
	 *         "page":"/user/userList",
	 *         "icon":"",
	 *         "zindex":"1100",
	 *         "checked":false,
	 *         "open":true
	 *     },
	 *     {
	 *         "id":"3",
	 *         "name":"角色管理",
	 *         "pId":"1",
	 *         "istype":"0",
	 *         "code":"rolemanage",
	 *         "page":"/auth/roleManage",
	 *         "icon":"",
	 *         "zindex":"1200",
	 *         "checked":false,
	 *         "open":true
	 *     },
	 *     {
	 *         "id":"4",
	 *         "name":"权限管理",
	 *         "pId":"1",
	 *         "istype":"0",
	 *         "code":"permmanage",
	 *         "page":"/auth/permList",
	 *         "icon":null,
	 *         "zindex":"1300",
	 *         "checked":false,
	 *         "open":true
	 *     },
	 *     {
	 *         "id":"15",
	 *         "name":"项目管理",
	 *         "pId":"0",
	 *         "istype":"0",
	 *         "code":"project",
	 *         "page":"/",
	 *         "icon":"projectManage.png",
	 *         "zindex":"1400",
	 *         "checked":false,
	 *         "open":true
	 *     },
	 *     {
	 *         "id":"17",
	 *         "name":"报告管理",
	 *         "pId":"15",
	 *         "istype":"1",
	 *         "code":"report",
	 *         "page":"/report/reportConfig",
	 *         "icon":null,
	 *         "zindex":"1400",
	 *         "checked":false,
	 *         "open":true
	 *     },
	 *     {
	 *         "id":"19",
	 *         "name":"Trace管理",
	 *         "pId":"15",
	 *         "istype":"0",
	 *         "code":"issuemanage",
	 *         "page":"/issue/issueList",
	 *         "icon":null,
	 *         "zindex":"1600",
	 *         "checked":false,
	 *         "open":true
	 *     },
	 *     {
	 *         "id":"20",
	 *         "name":"APP策略管理",
	 *         "pId":"15",
	 *         "istype":"0",
	 *         "code":"apppolicy",
	 *         "page":"/policy/appPolicyConfig",
	 *         "icon":null,
	 *         "zindex":"1400",
	 *         "checked":false,
	 *         "open":true
	 *     },
	 *     {
	 *         "id":"22",
	 *         "name":"项目列表",
	 *         "pId":"15",
	 *         "istype":"1",
	 *         "code":"projectmanager",
	 *         "page":"/projectManager/projectList",
	 *         "icon":null,
	 *         "zindex":"1000",
	 *         "checked":false,
	 *         "open":true
	 *     },
	 *     {
	 *         "id":"24",
	 *         "name":"工具管理",
	 *         "pId":"0",
	 *         "istype":"0",
	 *         "code":"tools",
	 *         "page":"/",
	 *         "icon":"toolManage.png",
	 *         "zindex":"1500",
	 *         "checked":false,
	 *         "open":true
	 *     },
	 *     {
	 *         "id":"25",
	 *         "name":"VR测试",
	 *         "pId":"24",
	 *         "istype":"1",
	 *         "code":"toolsmanager",
	 *         "page":"/tools/vrtest",
	 *         "icon":null,
	 *         "zindex":"1600",
	 *         "checked":false,
	 *         "open":true
	 *     },
	 *     {
	 *         "id":"28",
	 *         "name":"项目趋势",
	 *         "pId":"15",
	 *         "istype":"1",
	 *         "code":"projectmanager",
	 *         "page":"/projectManager/projectEchartLine",
	 *         "icon":null,
	 *         "zindex":"1020",
	 *         "checked":false,
	 *         "open":true
	 *     },
	 *     {
	 *         "id":"30",
	 *         "name":"APP自动测试",
	 *         "pId":"24",
	 *         "istype":"1",
	 *         "code":"autotest",
	 *         "page":"/test/autotest",
	 *         "icon":null,
	 *         "zindex":"1501",
	 *         "checked":false,
	 *         "open":true
	 *     },
	 *     {
	 *         "id":"31",
	 *         "name":"123",
	 *         "pId":"17",
	 *         "istype":"1",
	 *         "code":"12",
	 *         "page":"12",
	 *         "icon":null,
	 *         "zindex":"1122",
	 *         "checked":false,
	 *         "open":true
	 *     },
	 *     {
	 *         "id":"32",
	 *         "name":"333",
	 *         "pId":"17",
	 *         "istype":"0",
	 *         "code":"333",
	 *         "page":"333",
	 *         "icon":null,
	 *         "zindex":"33333",
	 *         "checked":false,
	 *         "open":true
	 *     },
	 *     {
	 *         "id":"33",
	 *         "name":"88888",
	 *         "pId":"0",
	 *         "istype":"0",
	 *         "code":"888",
	 *         "page":"888",
	 *         "icon":null,
	 *         "zindex":"88888",
	 *         "checked":false,
	 *         "open":true
	 *     },
	 *     {
	 *         "id":"34",
	 *         "name":"1111111",
	 *         "pId":"0",
	 *         "istype":"0",
	 *         "code":"1111",
	 *         "page":"1111",
	 *         "icon":null,
	 *         "zindex":"11111111",
	 *         "checked":false,
	 *         "open":true
	 *     }
	 * ]
	 * }
	 **/
	@GetMapping(value = "/findPerms")
	@ResponseBody
	public ResponseResult findPerms() {
		logger.debug("权限树列表！");
        ResponseResult responseResult = new ResponseResult(ResultCode.SUCCESS);
		List<PermissionVO> pvo = null;
		try {
			pvo = authService.findPerms();
			//生成页面需要的json格式
			logger.debug("权限树列表查询=pvo:{}" , pvo);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("list",pvo);
            responseResult.setData(jsonObject);
		} catch (Exception e) {
			logger.error("权限树列表查询异常！", e);
            responseResult = new ResponseResult(ResultCode.SYSTEM_INNER_ERROR);
		}
		return responseResult;
	}

	/**
	 * 添加角色并授权
	 * @return PermTreeDTO
	 */
	/**
	 * @api {post} /auth/addRole addRole
	 * @apiDescription 添加角色
	 * @apiName addRole
	 * @apiGroup Role manage
	 * @apiSuccessExample Success-Response:
	 * {
	 *
	 * }
	 **/
	@PostMapping(value = "/addRole")
	@ResponseBody
	public ResponseResult addRole(@RequestBody RoleDTO roleDTO) {
		ResponseResult responseResult;
		try {
			if(null == roleDTO || roleDTO.getPermIds() == null){
                responseResult = new ResponseResult(ResultCode.PARAM_NOT_COMPLETE);
				return responseResult;
			}
            roleDTO.setInsertTime(new Date());
			Role role = new Role();
			role.setId(roleDTO.getId());
            role.setInsertTime(roleDTO.getInsertTime());
            role.setUpdateTime(roleDTO.getUpdateTime());
            role.setCode(roleDTO.getCode());
            role.setDescpt(roleDTO.getDescpt());
            role.setInsertUid(roleDTO.getInsertUid());
            role.setRoleName(roleDTO.getRoleName());
			authService.addRole(role,roleDTO.getPermIds());
            responseResult = new ResponseResult(ResultCode.SUCCESS);
		} catch (Exception e) {
			logger.error("添加角色并授权！异常！", e);
            responseResult = new ResponseResult(ResultCode.SYSTEM_INNER_ERROR);
		}
		return responseResult;
	}

	/**
	 * 根据id查询角色
	 * @return PermTreeDTO
	 */
	/**
	 * @api {get} /auth/updateRole/{id} updateRole
	 * @apiDescription 根据id查询角色
	 * @apiName updateRole
	 * @apiGroup Role manage
	 * @apiParamExample {json} Request-Example:
	 * {
	 * 	id=1
	 * }
	 * @apiSuccessExample Success-Response:
	 * {
	 * }
	 **/
	@GetMapping(value = "/updateRole/{id}")
	//@ResponseBody
	public ModelAndView updateRole(@PathVariable("id") Integer id) {
		ModelAndView mv=new ModelAndView("/auth/roleManage");
		try {
			if(null==id){
				mv.addObject("msg","请求参数有误，请您稍后再试");
				return mv;
			}
			mv.addObject("flag","updateRole");
			RoleVO rvo=this.authService.findRoleAndPerms(id);
			//角色下的权限
			List<RolePermissionKey> rpks=rvo.getRolePerms();
			//获取全部权限数据
			List<PermissionVO> pvos = authService.findPerms();
			for (RolePermissionKey rpk : rpks) {
				//设置角色下的权限checked状态为：true
				for (PermissionVO pvo : pvos) {
					if(String.valueOf(rpk.getPermitId()).equals(String.valueOf(pvo.getId()))){
						pvo.setChecked(true);
					}
				}
			}
			mv.addObject("perms", pvos.toArray());
			//角色详情
			mv.addObject("roleDetail",rvo);
		} catch (Exception e) {
			logger.error("添加角色并授权！异常！", e);
			mv.addObject("msg","请求异常，请您稍后再试");
		}
		return mv;
	}

	/**
	 * 更新角色并授权
	 * @return PermTreeDTO
	 */
	/**
	 * @api {post} /auth/setRole setRole
	 * @apiDescription 更新角色
	 * @apiName setRole
	 * @apiGroup Role manage
	 * @apiParamExample {json} Request-Example:
	 * {
	 * 	id: 1
	 * 	rolePermIds: 1,2,3,4,15,17,19,20,22,28,24,25,30
	 * 	roleName: 超级管理员
	 * 	code: superman
	 * 	descpt: 超级管理员
	 * }
	 * @apiSuccessExample Success-Response:
	 * {
	 * 	"ok"
	 * }
	 **/
	@PostMapping(value = "/setRole")
	@ResponseBody
	public ResponseResult setRole(@RequestBody RoleDTO roleDTO) {
	    ResponseResult responseResult;
		try {
			if( null == roleDTO || roleDTO.getPermIds() == null){
                responseResult = new ResponseResult(ResultCode.PARAM_IS_BLANK);
				return responseResult;
			}
            Role role = new Role();
            role.setId(roleDTO.getId());
            role.setInsertTime(roleDTO.getInsertTime());
            role.setUpdateTime(new Date());
            role.setCode(roleDTO.getCode());
            role.setDescpt(roleDTO.getDescpt());
            role.setInsertUid(roleDTO.getInsertUid());
            role.setRoleName(roleDTO.getRoleName());
            authService.updateRole(role,roleDTO.getPermIds());
            responseResult = new ResponseResult(ResultCode.SUCCESS);
			return responseResult;
		} catch (Exception e) {
			logger.error("更新角色并授权！异常！", e);
            responseResult = new ResponseResult(ResultCode.SYSTEM_INNER_ERROR);
		}
		return responseResult;
	}

	/**
	 * 删除角色以及它对应的权限
	 * @param id
	 * @return
	 */
	/**
	 * @api {post} /auth/delRole delRole
	 * @apiDescription 删除角色
	 * @apiName delRole
	 * @apiGroup Role manage
	 * @apiParamExample {json} Request-Example:
	 * {
	 * 	id: 9
	 * }
	 * @apiSuccessExample Success-Response:
	 * {
	 * 	"ok"
	 * }
	 **/
	@PostMapping(value = "/delRole")
	@ResponseBody
	public ResponseResult delRole(@RequestBody JSONObject jsonParam) {
	    Integer id = Integer.valueOf(jsonParam.get("id").toString());
        ResponseResult responseResult;
		try {
			if (id > 0) {
                String msg = authService.delRole(id);
                if("ok".equals(msg)){
                    responseResult  = new ResponseResult(ResultCode.SUCCESS);
                }else {
                    responseResult  = new ResponseResult(ResultCode.FAIL);
                }
				return responseResult;
			}else{
                responseResult = new ResponseResult(ResultCode.PARAM_IS_INVALID);
            }
		} catch (Exception e) {
			logger.error("删除角色异常！", e);
            responseResult = new ResponseResult(ResultCode.SYSTEM_INNER_ERROR);
		}
		return responseResult;
	}

	/**
	 * 查找所有角色
	 * @return
	 */
	/**
	 * @api {post} /auth/getRoles getRoles
	 * @apiDescription 查找所有角色
	 * @apiName getRoles
	 * @apiGroup Role manage
	 * @apiParamExample {json} Request-Example:
	 * {
	 * 	id: 9
	 * }
	 * @apiSuccessExample Success-Response:
	 * {
	 * [
	 * {"id":1,
	 * "roleName":"超级管理员",
	 * "descpt":"超级管理员",
	 * "code":"superman",
	 * "insertUid":null,
	 * "insertTime":1564284533000,
	 * "updateTime":1589783457000},
	 * {"id":2,
	 * "roleName":"高级管理员",
	 * "descpt":"高级管理员",
	 * "code":"highmanage",
	 * "insertUid":null,
	 * "insertTime":1564284533000,
	 * "updateTime":1565405507000},
	 * {"id":3,
	 * "roleName":"项目经理",
	 * "descpt":"项目经理",
	 * "code":"pm",
	 * "insertUid":null,
	 * "insertTime":1564284533000,
	 * "updateTime":1565408595000},
	 * {"id":4,
	 * "roleName":"开发人员",
	 * "descpt":"开发人员",
	 * "code":"developers",
	 * "insertUid":null,
	 * "insertTime":1564284533000,
	 * "updateTime":1565167369000},
	 * {"id":5,
	 * "roleName":"测试人员",
	 * "descpt":"测试人员",
	 * "code":"tester",
	 * "insertUid":null,
	 * "insertTime":1564284533000,
	 * "updateTime":1565167311000},
	 * {"id":6,
	 * "roleName":"客户",
	 * "descpt":"客户",
	 * "code":"customer",
	 * "insertUid":null,
	 * "insertTime":1564284533000,
	 * "updateTime":1565166472000},
	 * {"id":7,
	 * "roleName":"供应商",
	 * "descpt":"供应商",
	 * "code":"supplier",
	 * "insertUid":null,
	 * "insertTime":1564284533000,
	 * "updateTime":1565167361000},
	 * {"id":8,
	 * "roleName":"产品经理",
	 * "descpt":"项目PO，负责项目需求整理和ID发掘",
	 * "code":"po",
	 * "insertUid":null,
	 * "insertTime":1564716518000,
	 * "updateTime":1565167349000}]
	 * }
	 **/
	@GetMapping(value = "/getRoles")
	@ResponseBody
	public ResponseResult getRoles() {
		logger.debug("查找所有角色!");
        ResponseResult responseResult = new ResponseResult(ResultCode.SUCCESS);
		List<Role> roles=null;
		try {
            roles = authService.getRoles();
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("list",roles);
            responseResult.setData(jsonObject);
			return responseResult;
		} catch (Exception e) {
			logger.error("查找所有角色异常！", e);
            responseResult = new ResponseResult(ResultCode.SYSTEM_INNER_ERROR);
		}
		return responseResult;
	}

	/**
	 * 根据用户id查询权限树数据
	 * @return PermTreeDTO
	 */
	/**
	 * @api {post} /auth/getUserPerms getUserPerms
	 * @apiDescription 根据用户id查询权限树数据
	 * @apiName getUserPerms
	 * @apiGroup Role manage
	 * @apiSuccessExample Success-Response:
	 *{
	 * [
	 *     {
	 *         "id":"1",
	 *         "name":"系统管理",
	 *         "pId":"0",
	 *         "istype":"0",
	 *         "code":"system",
	 *         "page":"/",
	 *         "icon":"systemManage.png",
	 *         "zindex":"100",
	 *         "checked":false,
	 *         "open":true
	 *     },
	 *     {
	 *         "id":"22",
	 *         "name":"项目列表",
	 *         "pId":"15",
	 *         "istype":"1",
	 *         "code":"projectmanager",
	 *         "page":"/projectManager/projectList",
	 *         "icon":null,
	 *         "zindex":"1000",
	 *         "checked":false,
	 *         "open":true
	 *     },
	 *     {
	 *         "id":"28",
	 *         "name":"项目趋势",
	 *         "pId":"15",
	 *         "istype":"1",
	 *         "code":"projectmanager",
	 *         "page":"/projectManager/projectEchartLine",
	 *         "icon":null,
	 *         "zindex":"1020",
	 *         "checked":false,
	 *         "open":true
	 *     },
	 *     {
	 *         "id":"2",
	 *         "name":"用户管理",
	 *         "pId":"1",
	 *         "istype":"0",
	 *         "code":"usermanage",
	 *         "page":"/user/userList",
	 *         "icon":"",
	 *         "zindex":"1100",
	 *         "checked":false,
	 *         "open":true
	 *     },
	 *     {
	 *         "id":"3",
	 *         "name":"角色管理",
	 *         "pId":"1",
	 *         "istype":"0",
	 *         "code":"rolemanage",
	 *         "page":"/auth/roleManage",
	 *         "icon":"",
	 *         "zindex":"1200",
	 *         "checked":false,
	 *         "open":true
	 *     },
	 *     {
	 *         "id":"4",
	 *         "name":"权限管理",
	 *         "pId":"1",
	 *         "istype":"0",
	 *         "code":"permmanage",
	 *         "page":"/auth/permList",
	 *         "icon":null,
	 *         "zindex":"1300",
	 *         "checked":false,
	 *         "open":true
	 *     },
	 *     {
	 *         "id":"17",
	 *         "name":"报告管理",
	 *         "pId":"15",
	 *         "istype":"1",
	 *         "code":"report",
	 *         "page":"/report/reportConfig",
	 *         "icon":null,
	 *         "zindex":"1400",
	 *         "checked":false,
	 *         "open":true
	 *     },
	 *     {
	 *         "id":"20",
	 *         "name":"APP策略管理",
	 *         "pId":"15",
	 *         "istype":"0",
	 *         "code":"apppolicy",
	 *         "page":"/policy/appPolicyConfig",
	 *         "icon":null,
	 *         "zindex":"1400",
	 *         "checked":false,
	 *         "open":true
	 *     },
	 *     {
	 *         "id":"15",
	 *         "name":"项目管理",
	 *         "pId":"0",
	 *         "istype":"0",
	 *         "code":"project",
	 *         "page":"/",
	 *         "icon":"projectManage.png",
	 *         "zindex":"1400",
	 *         "checked":false,
	 *         "open":true
	 *     },
	 *     {
	 *         "id":"24",
	 *         "name":"工具管理",
	 *         "pId":"0",
	 *         "istype":"0",
	 *         "code":"tools",
	 *         "page":"/",
	 *         "icon":"toolManage.png",
	 *         "zindex":"1500",
	 *         "checked":false,
	 *         "open":true
	 *     },
	 *     {
	 *         "id":"30",
	 *         "name":"APP自动测试",
	 *         "pId":"24",
	 *         "istype":"1",
	 *         "code":"autotest",
	 *         "page":"/test/autotest",
	 *         "icon":null,
	 *         "zindex":"1501",
	 *         "checked":false,
	 *         "open":true
	 *     },
	 *     {
	 *         "id":"25",
	 *         "name":"VR测试",
	 *         "pId":"24",
	 *         "istype":"1",
	 *         "code":"toolsmanager",
	 *         "page":"/tools/vrtest",
	 *         "icon":null,
	 *         "zindex":"1600",
	 *         "checked":false,
	 *         "open":true
	 *     },
	 *     {
	 *         "id":"19",
	 *         "name":"Trace管理",
	 *         "pId":"15",
	 *         "istype":"0",
	 *         "code":"issuemanage",
	 *         "page":"/issue/issueList",
	 *         "icon":null,
	 *         "zindex":"1600",
	 *         "checked":false,
	 *         "open":true
	 *     }
	 * ]
	 * }
	 **/
	@GetMapping(value = "/getUserPerms")
	@ResponseBody
	public ResponseResult getUserPerms() {
        logger.debug("根据用户id查询限树列表！");
        ResponseResult responseResult = new ResponseResult(ResultCode.SUCCESS);
		List<PermissionVO> pvo = null;
		User existUser= (User) SecurityUtils.getSubject().getPrincipal();
		if(null==existUser){
			logger.debug("根据用户id查询限树列表！用户未登录");
            responseResult = new ResponseResult(ResultCode.USER_NOT_LOGGED_IN);
			return responseResult;
		}
		try {
			pvo = authService.getUserPerms(existUser.getId());
			JSONObject jsonObject = new JSONObject();
			jsonObject.put("list", pvo);
			responseResult.setData(jsonObject);
			//生成页面需要的json格式
		} catch (Exception e) {
			logger.error("根据用户id查询权限树列表查询异常！", e);
            responseResult = new ResponseResult(ResultCode.SYSTEM_INNER_ERROR);
		}
		return responseResult;
	}

	@GetMapping(value = "/getPermsByRoleId")
	@ResponseBody
	public ResponseResult getPermsByRoleId(@RequestParam("roleId") Integer roleId) {
		logger.debug("根据角色id查询限树列表！");
		return authService.getPermsByRoleId(roleId);
	}
}
